package com.eryos.android.cigarettecounter.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.os.Environment;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public abstract class IOStream {

	protected boolean externalStorageIsAvailable = false;
	protected boolean externalStorageIsWriteable = false;
	protected enum TYPE{READ, WRITE}

	protected File path;
	protected File outputFile;
	
	protected String EXTERN_NAME = "output.csv";


	
	public IOStream(){
		
	}
	
	/**
	* This method check if the external media is available or not ( in read or write mode )
	*/
	public void checkStorageAvailability(){

		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    externalStorageIsAvailable = externalStorageIsWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    externalStorageIsAvailable = true;
		    externalStorageIsWriteable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    externalStorageIsAvailable = externalStorageIsWriteable = false;
		}
	}

	
	public boolean openStream(TYPE _type) throws IOException{
		try {
		checkStorageAvailability();

		if ( ( _type.equals(TYPE.READ) && externalStorageIsAvailable ) || 
				( _type.equals(TYPE.WRITE) && externalStorageIsAvailable && externalStorageIsWriteable ) ) {

			System.out.println(android.os.Build.VERSION.SDK_INT);
			
			if ( android.os.Build.VERSION.SDK_INT > 7 ) {
				path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
			}else{
				path = Environment.getExternalStorageDirectory();
				path = new File(path.getAbsolutePath()+"/Download");
			}
		
			outputFile = new File(path, EXTERN_NAME);
			
			if ( path.exists() || path.mkdirs()) {
				if ( outputFile.exists() ){
					outputFile.delete();
				}
				
				return true;
			}
			else {
				System.out.println("path.mkdirs() fails !!!");
				return false;
			}
		}else{
			System.out.println("SD not available !!!");
			return false;
		}
		
		} catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	
	public abstract void closeStream() throws IOException;

}
